﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Text;
using System.Web.UI.WebControls;
using RainyPWS.Common;

namespace RainyPWS.Web.Admin.controls
{
    public partial class Pager : System.Web.UI.UserControl, IPostBackEventHandler
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            ClientScriptManager cs = Page.ClientScript;

            int PageCount = (int)Math.Ceiling((double)RecordCount / (double)PageSize);
            if (PageCount > 0)
            {
                if (PageIndex < 1)
                    PageIndex = 1;
                if (PageIndex > PageCount)
                    PageIndex = PageCount;
            }
            else
            {
                PageIndex = 0;
            }
            lblCount.Text = RecordCount.ToString();
            lblPageCount.Text = PageCount.ToString();
            txtPageIndex.Text = PageIndex.ToString();
            if (PageCount > 0)
            {
                if (PageIndex > 1)
                {
                    lblFirst.CssClass = "btnPage btnFristEnabled";
                    lblFirst.Attributes.Add("onclick", cs.GetPostBackEventReference(this, "First"));
                    lblPrev.CssClass = "btnPage btnPrevEnabled";
                    lblPrev.Attributes.Add("onclick", cs.GetPostBackEventReference(this, "Prev"));
                }
                else
                {
                    lblFirst.CssClass = "btnPage btnFristDisabled";
                    lblFirst.Attributes.Remove("onclick");
                    lblPrev.CssClass = "btnPage btnPrevDisabled";
                    lblPrev.Attributes.Remove("onclick");
                }
                if (PageIndex < PageCount)
                {
                    lblNext.CssClass = "btnPage btnNextEnabled";
                    lblNext.Attributes.Add("onclick", cs.GetPostBackEventReference(this, "Next"));
                    lblLast.CssClass = "btnPage btnLastEnabled";
                    lblLast.Attributes.Add("onclick", cs.GetPostBackEventReference(this, "Last"));
                }
                else
                {
                    lblNext.CssClass = "btnPage btnNextDisabled";
                    lblNext.Attributes.Remove("onclick");
                    lblLast.CssClass = "btnPage btnLastDisabled";
                    lblLast.Attributes.Remove("onclick");
                }
                txtPageIndex.Enabled = true;
            }
            else
            {
                lblFirst.CssClass = "btnPage btnFristDisabled";
                lblFirst.Attributes.Remove("onclick");
                lblPrev.CssClass = "btnPage btnPrevDisabled";
                lblPrev.Attributes.Remove("onclick");
                lblNext.CssClass = "btnPage btnNextDisabled";
                lblNext.Attributes.Remove("onclick");
                lblLast.CssClass = "btnPage btnLastDisabled";
                lblLast.Attributes.Remove("onclick");
                txtPageIndex.Enabled = false;
            }
            if (!IsPostBack)
            {
                lblRefresh.CssClass = "btnPage btnRefresh";
                lblRefresh.Attributes.Add("onclick", cs.GetPostBackEventReference(this, "Refresh"));
                txtPageIndex.Attributes.Add("onkeypress", @"return (/[\d]/.test(String.fromCharCode(event.keyCode)))");
            }
        }

        public void RaisePostBackEvent(string eventArgument)
        {
            if (PagerIndexChanged != null)
            {
                PagerCommandType cmd;
                int pageindex = PageIndex;
                switch (eventArgument.ToLower())
                {
                    case "first":
                        cmd = PagerCommandType.First;
                        break;
                    case "prev":
                        cmd = PagerCommandType.Prev;
                        break;
                    case "next":
                        cmd = PagerCommandType.Next;
                        break;
                    case "last":
                        cmd = PagerCommandType.Last;
                        break;
                    case "refresh":
                        cmd = PagerCommandType.Refresh;
                        break;
                    case "num":
                        cmd = PagerCommandType.Num;
                        pageindex = Utils.StrToInt(txtPageIndex.Text, PageIndex);
                        break;
                    default:
                        cmd = PagerCommandType.First;
                        break;
                }
                PagerIndexChanged(this, new PagerCommandEventArgs(cmd, pageindex));
            }
        }

        protected void txtPageIndex_TextChanged(object sender, EventArgs e)
        {
            if (PagerIndexChanged != null)
            {
                PagerCommandType cmd = PagerCommandType.Num;
                int pageindex = Utils.StrToInt(txtPageIndex.Text, PageIndex);
                PagerIndexChanged(this, new PagerCommandEventArgs(cmd, pageindex));
            }
        }

        #region Properties
        public int RecordCount
        {
            get
            {
                if (ViewState["recordcount"] != null)
                {
                    return (int)ViewState["recordcount"];
                }
                else
                {
                    return 0;
                }
            }
            set { ViewState["recordcount"] = value; }
        }

        public int PageSize
        {
            get
            {
                if (ViewState["pagesize"] != null)
                {
                    return (int)ViewState["pagesize"];
                }
                else
                {
                    return 10;
                }
            }
            set { ViewState["pagesize"] = value; }
        }

        public int PageIndex
        {
            get
            {
                if (ViewState["pageindex"] != null)
                {
                    return (int)ViewState["pageindex"];
                }
                else
                {
                    return 0;
                }
            }
            set { ViewState["pageindex"] = value; }
        }

        public event PagerIndexEventHandler PagerIndexChanged;
        #endregion
    }

    public delegate void PagerIndexEventHandler(object sender, PagerCommandEventArgs e);

    public class PagerCommandEventArgs : EventArgs
    {
        private PagerCommandType _command;
        private int _pageIndex;
        public PagerCommandEventArgs(PagerCommandType cmd, int pageIndex)
        {
            _command = cmd;
            _pageIndex = pageIndex;
        }

        public PagerCommandType Command
        {
            get { return _command; }
        }

        public int PageIndex
        {
            get { return _pageIndex; }
        }
    }
}